// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ADS } = require("../ads/ADS");
const { videoAds } = require("../ads/video");
const { show } = require("../game/alert");
const { shopData, player } = require("../shop/gameData");

cc.Class({
    extends: cc.Component,

    properties: {
        nanping:cc.Node,
        ningde:cc.Node,
        sanming:cc.Node,
        fuzhou:cc.Node,
        longyan:cc.Node,
        quanzhou:cc.Node,
        putian:cc.Node,
        zhangzhou:cc.Node,
        xiamen:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable () {
        this.scheduleOnce(() => {
            this.getShopList();
        },0.2)
    },

    start () {
        
    },
    getShopList(){
        // let myShop  = cc.sys.localStorage.getItem('shopList');
        // if(!myShop){
                //遍历修改样式
                let list = shopData.shopList;
                list.forEach(val=>{
                    if(val.status==1 || val.status == 2){
                        this[val.id]["color"] = cc.Color.ORANGE;
                    }
                })
        // }
    },
    openShop(target,id){
        let shop = shopData.shopList.find(val=>val.id==id);
        if(shop.status == 1 || shop.status == 2) return show("小秘提示",'店铺已开通，快去经营吧');
        if(player.money>=shop.startMoney){
            show('小秘提示',"开通店铺吗？需要"+formatMoney(shop.startMoney),(res)=>{
                if(res){
                    player.money -= shop.startMoney;
                    shop.status = 2;
                    target.currentTarget.color = cc.Color.ORANGE;
                    setTimeout(()=>{
                        show("小秘提示",'店铺已开通，快去经营吧');
                    },400)
                    
                }else{
                    return false;
                }
            })
        }else{
            show('小秘提示',"金钱不足噢，是否观看广告解锁？", (r)=>{
                if(r){
                    videoAds.showAd((reward)=>{
                        if(reward){
                            shop.status = 2;
                            target.currentTarget.color = cc.Color.ORANGE;
                        }
                    });
                }
            })
        }
    }
    // update (dt) {},
});
